<?php

//$resultCount = mysql_result(mysql_query(" SELECT COUNT(*) FROM `airplay` "),0);
//echo('<h1>ct: '.$resultCount.'</h1>');

//alb rate documented in stats section of /includes/libccramp.inc 
// inputs: min-air-datetime, max-air-datetime, result-max, cat-id, status, min-add-date, request

$albRank = airAlbRank($statStartTime);
// {{{we need to swap an array so we can get spins}}}
//
//
if ($albRank) {
   for ($i=0; $i <= count($albRank); $i++) {
      $albRankId[$i] = $albRank[$i][albId];
   }
}
//if ($albRank) {
//   $i=0;
//   foreach ($albRank[albId] as $alb) {
//      $alb
//   
//   }
//}

//function getAlbSpinsFromRank ($albId) {
//   
//}

//if ($albRank) {
//  foreach ($albRank as $key => $row) {
//     
//  }
//}

?><pre><?php 

var_dump($albRank);

?></pre><?php 

if ($_REQUEST[searchFor] === 'Search') $srchFor = '';

if ($_POST['promoter']) $where = "WHERE (`promoter`='".$_POST['promoter']."' OR `promoter2`='".$_POST['promoter']."' OR `promoter3`='".$_POST['promoter']."') ";

if (!$_POST['status']) {
	if (!$where) $where = " WHERE ";
	else $where .= " AND ";
	$where .= " (`status`<'3') ";
} else {
	if (!$where) $where = " WHERE ";
	else $where .= " AND ";
	$where .= " (`status`='".mysql_real_escape_string($_POST['status'])."') ";
}

if (!$srchFor) {

   if (!$where) $where = ' WHERE ';
   else $where .= ' AND ';
   
   switch ($_REQUEST[srchType]) {
   default:
      $where .= " ((LOWER(`artist`) LIKE '%".mysql_real_escape_string($srchFor)."%') OR (LOWER(`title`) LIKE '%".mysql_real_escape_string($_POST['searchFor'])."%')) ";
      break;
//   case 'Search':
//      $where .= " ((LOWER(`artist`) LIKE '%".mysql_real_escape_string($srchFor)."%') OR (LOWER(`title`) LIKE '%".mysql_real_escape_string($_POST['searchFor'])."%')) ";
//      break;
   case 'art':
      $where .= " ((LOWER(`artist`) LIKE '%".mysql_real_escape_string($srchFor)."%')) ";
      break;
   case 'alb':
      $where .= " ((LOWER(`title`) LIKE '%".mysql_real_escape_string($srchFor)."%')) ";
      break;
   case 'alb':
      $where .= " ((LOWER(`label`) LIKE '%".mysql_real_escape_string($srchFor)."%')) ";
      break;
   }
}

//if ($_POST['searchFor'] && $_POST['searchFor'] !== 'Search') $where .= " AND ((LOWER(`artist`) LIKE '%".mysql_real_escape_string($_POST['searchFor'])."%') OR (LOWER(`title`) LIKE '%".mysql_real_escape_string($_POST['searchFor'])."%')) ";

switch ($_REQUEST[orderBy]) {
default:
   $orderBy = " ORDER BY TRIM(LEADING 'the ' FROM LOWER(`artist`)) ASC ";
   break;
case 'artAsc':
   $orderBy = " ORDER BY TRIM(LEADING 'the ' FROM LOWER(`artist`)) ASC ";
   break;
case 'albAsc':
   $orderBy = " ORDER BY TRIM(LEADING 'the ' FROM LOWER(`title`)) ASC ";
   break;
case 'addDateAsc':
   $orderBy = " ORDER BY `add_date` ASC ";
   break;
case 'addDateDesc':
   $orderBy = " ORDER BY `add_date` DESC ";
   break;
}

$query = "SELECT * FROM `album` ". $where . $orderBy;

$result = mysql_query($query) or die(mysql_error());
?>
<form action="<?=$_SERVER[PHP_SELF]?>" method="post">
<fieldset>
<legend>Display Options</legend>

<table>
<tr>
<td><input type="text" name="srchFor" value="Search" onBlur="if (value == '') {value = 'Search'}" onFocus="if (value == 'Search') {value =''}" value="Search"  />
<br />
<select name="srchType">
<option value="">Search By:</option>
<option value="art">Artist</option>
<option value="alb">Album</option>
<option value="label">Label</option>
</select>
<br />

<input type="hidden" name="submit" value="true" />
<input type="hidden" name="mode" value="cdlistedit" /><br />
<input type="submit" value="Go Go!" />
</td>
<td><select name="promoter" style="width: 110px;">
<option value="" selected="selected">promoter:</option>
<?php
$promoSql = "SELECT `id`,`nickname`,`company` FROM `promoter` ORDER BY `company`";
$promoQuery = mysql_query($promoSql);
while ($promoter_row = mysql_fetch_row($promoQuery)) {
?><option value="<?=$promoter_row[0]?>" <?php if ($_POST['promoter'] == $promoter_row[0]) echo('selected="selected"') ?>><?=$promoter_row[1]?>@<?=$promoter_row[2]?></option>
<?php
}
?></select>
<br /><select name="status" style="width: 110px;">
<option value="" selected="selected">Status:</option>
<option value="0">Not in</option>
<option value="1">In review</option>
<option value="2">In Rotation</option>
<option value="3">Old</option>
<option value="4">Archived</option>
<option value="5">Gone.</option>
</select>
<br />
<select name="orderBy" style="width: 110px;">
<option value="">Order By:</option>
<option value="artAsc">Artist</option>
<option value="albAsc">Album</option>
<option value="addDateAsc">Add Date (ascending)</option>
<option value="addDateDesc">Add Date (descending)</option>
</select>
</td>
</tr>
</table>
</fieldset>
</form>

<?php 
if ($_REQUEST[submit] === 'true') {
?>
<table>
<tr>
<th colspan="4"></th><th colspan="2">This Week</th><th colspan="3">Last Week</th>
</tr><tr>
<th>Artist</th><th>Title</th><th>Promoters</th><th>Spins</th><th>Rotation</th><th>Rank</th><th>Spins</th><th>Rotation</th>
</tr>
<?php
while($row = mysql_fetch_assoc($result)) { //show all the results from the above query
?>
<tr class="highlighted">
<td><input type="checkbox" /><a href="?mode=cdedit&amp;cddbid=<?=$row[cddbid]?>&amp;cat=<?=$cat?>" title="Edit this CD"><img style="border: 0px;" height="16" width="16" title="Edit this CD" alt="edit this CD" src="./themes/<?=$theme?>/img/edit.png" /></a><?=trunc($row[artist], 25)?></td>
<td><?=trunc($row[title], 20)?></td>
<td></td>
<!-- <?=$row[status]?> -->
<td><?php
$temp_row = mysql_fetch_array(mysql_query("SELECT `spins` FROM `temp_stat` WHERE `cdid`='$row[cddbid]' LIMIT 1"));
//if (!$temp_row[spins]) $temp_row[spins] = 0;
//echo($temp_row[spins]);
echo(get_spins($row[cddbid], $StatStart, $nowstamp));
?></td>
<td><?php 
$albIdRow = array_search($row[cddbid], $albRankId);
if ($albIdRow||$albIdRow === 0 ) echo ($albRank[$albIdRow][spins]);
?>(<? echo(get_spins($row[cddbid], $StatStart2, $StatStart)) ?> last week) ... (<?=get_weeks($row[cddbid])?> wks.)</td>
</tr>
<? 
}
?>
</table>
<?php
}
//require('stats_calc2.inc');
?>